package com.example.demo.web;

import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONUtil;
import com.example.demo.api.service.CustomService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 页面跳转
 *
 * @author yuanzheng
 * @date 2022-01-13
 */
@Controller
public class WebController {

    @Resource
    private CustomService customService;

    /**
     * 首页
     *
     * @return 页面路径
     */
    @GetMapping({"/", "/index"})
    public String index() {
        return "index/index";
    }

    /**
     * 可视化编辑器页面
     *
     * @return 页面路径
     */
    @GetMapping({"/amisEditor"})
    public String amisEditor() {
        return "amis/amisEditor";
    }

    /**
     * 动态管理页面
     *
     * @return 页面路径
     */
    @GetMapping({"/pageManagement"})
    public String pageManagement() {
        return "amis/pageManagement";
    }

    /**
     * 动态页面
     *
     * @return 页面路径
     */
    @GetMapping({"/dynamicPage"})
    public ModelAndView dynamicPage(String id) {
        // 指定跳转到的页面
        ModelAndView modelAndView = new ModelAndView("amis/dynamicPage");
        List<Map<String, Object>> query = customService.queryById(id);
        Map<String, Object> map = query.get(0);
        String amisJson = MapUtil.getStr(map, "amisJson");
        // 添加要传递的数据
        modelAndView.addObject("amisJson", JSONUtil.toJsonStr(amisJson));
        return modelAndView;
    }
}
